{% extends 'airshow/base.html' %}
{% load static %}
{% load data_filters %}

{% block title %}数据管理 - {{ table }}{% endblock %}

{% block content %}
<div class="container mt-4">
    <div class="card">
        <div class="card-header bg-primary text-white d-flex justify-content-between align-items-center">
            <h4 class="mb-0">数据管理 - {{ table }}</h4>
            <div>
                <a href="{% url 'airshow:add_data' table=table %}" class="btn btn-success">
                    <i class="fas fa-plus"></i> 添加
                </a>
                <a href="{% url 'airshow:export_data' table=table %}" class="btn btn-info">
                    <i class="fas fa-download"></i> 导出
                </a>
                <a href="{% url 'airshow:import_data' table=table %}" class="btn btn-warning">
                    <i class="fas fa-upload"></i> 导入
                </a>
            </div>
        </div>
        <div class="card-body">
            <div class="table-responsive">
                <table class="table table-striped table-hover">
                    <thead>
                        <tr>
                            {% for column in columns %}
                            <th>{{ column }}</th>
                            {% endfor %}
                            <th>操作</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for item in data %}
                        <tr>
                            {% for column in columns %}
                            <td>{{ item|get_item:column }}</td>
                            {% endfor %}
                            <td>
                                <div class="btn-group">
                                    <a href="{% url 'airshow:edit_data' table=table pk=item|get_primary_key_value:columns %}" 
                                       class="btn btn-sm btn-primary">
                                        <i class="fas fa-edit"></i> 编辑
                                    </a>
                                    <a href="{% url 'airshow:delete_data' table=table pk=item|get_primary_key_value:columns %}" 
                                       class="btn btn-sm btn-danger">
                                        <i class="fas fa-trash"></i> 删除
                                    </a>
                                </div>
                            </td>
                        </tr>
                        {% empty %}
                        <tr>
                            <td colspan="{{ columns|length|add:1 }}" class="text-center">暂无数据</td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
{% endblock %}

{% block extra_js %}
<script>
    // 自定义模板过滤器函数
    function getItem(obj, key) {
        return obj[key];
    }
    
    function getPrimaryKey(columns) {
        // 假设第一列是主键
        return columns[0];
    }
    
    function getPrimaryKeyValue(columns, item) {
        return item[columns[0]];
    }
</script>
{% endblock %} 